import logging.config
from pynput.mouse import Controller as mController
from pynput.keyboard import Controller as kController
from pynput.keyboard import Key
import functools
import cv2 as cv
import numpy
import numpy as np
from PIL import ImageGrab, Image
import time
from cnocr import CnOcr


# 将来简化一点代码
def sleep(s):
    # 唯一一个地方的例外
    time.sleep(s)
    return


def ocrBloodPercentageNumber(x1, y1, w, h, sleeps):
    # (195, 38, 195 + 34,38 + 16)
    sleep(sleeps)
    # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
    ocr = CnOcr()

    # 魔兽图像的血量的绝对坐标
    # imTopBlood = ImageGrab.grab(
    #     bbox=(195, 38, 195 + 34,
    #           38 + 16))  # X1,Y1,X2,Y2
    imTopBlood = ImageGrab.grab(
        bbox=(x1, y1, x1 + w,
              y1 + h))  # X1,Y1,X2,Y2

    ptBloodLeftTopBlood = (0, 0)
    ptBloodRightBottom = (40, 40)
    point_color = (0, 0, 255)  # BGR
    thickness = 1
    lineType = 4
    imgYehaibo = cv.rectangle(numpy.asarray(imTopBlood), ptBloodLeftTopBlood, ptBloodRightBottom, point_color,
                              thickness, lineType)
    cv.imwrite(r'd:/yehaibo_Blood.jpg', imgYehaibo)
    # res = ocr.ocr(r'd:/yehaibo_Blood.jpg')
    # 直接从内存读取，不需要从磁盘文件读取
    res = ocr.ocr(imgYehaibo)
    wowStr = ''
    # print(res[0][0])
    for everyOne in res[0][0]:
        wowStr = wowStr + str(everyOne)
    wowStr = wowStr.replace('%', '')
    print(wowStr)
    # print(datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))
    return wowStr


if __name__ == '__main__':
    debug1 = ocrBloodPercentageNumber(195, 38, 34, 16, 5)
    print(debug1)
